package com.iszhouhua.blog.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iszhouhua.blog.common.annotation.CurrentUser;
import com.iszhouhua.blog.common.constant.CodeEnum;
import com.iszhouhua.blog.common.constant.Const;
import com.iszhouhua.blog.common.util.PBKDF2Utils;
import com.iszhouhua.blog.common.util.Result;
import com.iszhouhua.blog.common.util.ValidatorUtils;
import com.iszhouhua.blog.model.Category;
import com.iszhouhua.blog.model.Picture;
import com.iszhouhua.blog.model.User;
import com.iszhouhua.blog.service.CategoryService;
import com.iszhouhua.blog.service.PictureService;
import com.iszhouhua.blog.service.TagService;
import com.iszhouhua.blog.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.NumberUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Objects;

/**
 * 图片管理
 *
 * @author xiaojin
 * @since 2020-10-10
 */
@RestController
@RequestMapping("api/picture")
public class ApiPictureController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private PictureService pictureService;

  /**
   * 分页查询
   * @param page
   * @return
   */
    @GetMapping("list")
    public Result list(Page<Picture> page, Picture picture) {
      String description = picture.getDescription();
//      picture.setDescription(null);
      Long categoryId = picture.getCategoryId();

      QueryWrapper<Picture> queryWrapper = new QueryWrapper<>();
      if (StringUtils.isNotBlank(description)){
        queryWrapper.like("description",description);
      }
      if (categoryId != null){
        queryWrapper.eq("category_id",categoryId);
      }
      IPage<Picture> pictureIPage = pictureService.page(page, queryWrapper);
      for (Picture picture1 : pictureIPage.getRecords()){
        Category category = categoryService.getById(picture1.getCategoryId());
        picture1.setCategory(category);
      }
      return Result.success("查询成功", pictureIPage);
    }

    /**
     * 添加图片
     *
     * @param picture
     * @return
     * @throws Exception
     */
    @PostMapping
    public Result save(@RequestBody Picture picture) throws Exception {
      picture.setCreateTime(new Date());
      picture.setUpdateTime(new Date());
      boolean b = pictureService.save(picture);
      if (!b){
        return Result.fail("新增失败", null);
      }
        return Result.success("新增成功", null);
    }

  /**
   * 修改图片
   * @param picture
   * @param session
   * @return
   */
    @PutMapping
   public Result update(@RequestBody Picture picture, HttpSession session) {
      Picture oldPicture = pictureService.findPictureById(picture.getId());
      UpdateWrapper<Picture> wrapper = new UpdateWrapper<>();
      BeanUtils.copyProperties(picture, oldPicture);
      oldPicture.setUpdateTime(new Date());
      boolean b = pictureService.update(oldPicture, wrapper.eq("id", picture.getId()));
      if (!b){
        return Result.fail("修改失败",null);
      }
      return Result.success("修改成功", oldPicture);
    }

  /**
   * 根据id获取图片
   * @param id
   * @return
   */
    @GetMapping
    public Result info(Long id) {
      Picture picture = pictureService.findPictureById(id);
      Category category = categoryService.getById(picture.getCategoryId());
      picture.setCategory(category);
      return Result.success("获取成功", picture);
    }

  /**
   * 删除
   * @param id
   * @return
   */
    @DeleteMapping
    public Result delete(Long id){
      boolean remove = pictureService.removeById(id);
      if (remove){
        return Result.success("删除成功");
      }
      return Result.success("删除失败或者改图片不存在");
    }
}
